package com.fc.campusapplicationsystem.controller;

import com.fc.campusapplicationsystem.model.Serviceman;
import com.fc.campusapplicationsystem.service.ServicemanService;
import com.fc.campusapplicationsystem.util.ResponseOBJ;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 贺哲
 * @2020-02-29 15:29
 */
@RestController
@RequestMapping("/servicemanController")
public class ServicemanController {
    @Autowired
    ServicemanService servicemanService;

    @GetMapping("/list")
    public ResponseOBJ<List<Serviceman>> list(@RequestParam("page") Integer page,
                                              @RequestParam("limit") Integer limit) {
        ResponseOBJ<List<Serviceman>> responseOBJ = ResponseOBJ.getInstance();
        try {
            PageHelper.startPage(page, limit);
            List<Serviceman> servicemen = servicemanService.list();
            PageInfo<Serviceman> pageInfo = new PageInfo<>(servicemen);
            responseOBJ.setData(servicemen);
            responseOBJ.setCount(pageInfo.getTotal());
            responseOBJ.setMsg("查询成功");
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            responseOBJ.setMsg("查询失败");
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }

    @GetMapping("/all")
    public ResponseOBJ<List<Serviceman>> all(){
        ResponseOBJ<List<Serviceman>> responseOBJ = ResponseOBJ.getInstance();
        try {
            List<Serviceman> servicemen = servicemanService.list();
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
            responseOBJ.setMsg("查询成功");
            responseOBJ.setData(servicemen);
        } catch (Exception e){
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
            responseOBJ.setMsg("查询失败");
        }
        return responseOBJ;
    }


    /**
     * 新增维修员
     */
    @PostMapping("/add")
    public ResponseOBJ<Integer> add(@RequestBody Serviceman serviceman) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            if (servicemanService.checkUsername(serviceman)){
            Integer result = servicemanService.add(serviceman);
            responseOBJ.setData(result);
            responseOBJ.setMsg("添加成功!");
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
            }else {
                responseOBJ.setCode(ResponseOBJ.ERROR);
                responseOBJ.setMsg("添加失败,登录名已存在!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
            responseOBJ.setMsg("添加失败,请稍后再重试!");
        }
        return responseOBJ;
    }

    /**
     * 修改维修员
     */
    @PostMapping("/update")
    public ResponseOBJ<Integer> update(@RequestBody Serviceman serviceman) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            if (servicemanService.checkUsername(serviceman)){
                Integer result = servicemanService.update(serviceman);
                responseOBJ.setData(result);
                responseOBJ.setMsg("修改成功!");
                responseOBJ.setCode(ResponseOBJ.SUCCESS);
            }else {
                responseOBJ.setCode(ResponseOBJ.ERROR);
                responseOBJ.setMsg("修改失败,登录名已存在!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
            responseOBJ.setMsg("修改失败,请稍后再重试!");
        }
        return responseOBJ;
    }

    @GetMapping("/delete")
    public ResponseOBJ<Integer> delete(@RequestParam("servicemanId") Integer id) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            Integer result = servicemanService.delete(id);
            responseOBJ.setData(result);
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }
}
